<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head th:include="common_include :: commonHead('头像修改')"/>
<link th:href="@{/libs/cropbox/cropbox.css}" rel="stylesheet"/>
<body class="white-bg">
<input name="rowKey" id="rowKey" type="hidden" th:value="${user.rowKey}"/>
<input name="imgSrc" id="imgSrc" type="hidden" th:value="(${user.avatar} == null or ${user.avatar} == '') ? '/avatar/profile.jpg' : '/profile/avatar/' + ${user.avatar}">
<div class="container">
    <div class="imageBox">
        <div class="thumbBox"></div>
        <div class="spinner" style="display: none">Loading...</div>
    </div>
    <div class="action">
        <div class="new-contentarea tc">
            <a href="javascript:void(0)" class="upload-img"> <label for="avatar">选择图像</label> </a>
            <input type="file" class="" name="avatar" id="avatar" accept="image/*"/>
        </div>
        <input type="button" id="btnCrop" class="Btnsty_peyton" value="裁切"/>
        <input type="button" id="btnZoomIn" class="Btnsty_peyton" value="+"/>
        <input type="button" id="btnZoomOut" class="Btnsty_peyton" value="-"/>
    </div>
    <div class="cropped"></div>
</div>
<div th:include="common_include :: commonFoot"/>
<script th:src="@{/libs/cropbox/cropbox.js}"></script>
<script type="text/javascript">
    var options = {
        thumbBox: '.thumbBox',
        spinner: '.spinner',
        imgSrc: $("#imgSrc").val()
    }
    var cropper = $('.imageBox').cropbox(options);
    $(function () {
        $('#avatar').on('change',
            function () {
                var reader = new FileReader();
                reader.onload = function (e) {
                    options.imgSrc = e.target.result;
                    //根据MIME判断选择的文件是不是图片类型
                    if ((options.imgSrc).indexOf("image/") == -1) {
                        parent.layer.alert("文件格式错误，请选择图片类型，如：JPG,JEPG，PNG后缀的文件。", {icon: 2, title: "系统提示"});
                    } else {
                        cropper = $('.imageBox').cropbox(options);
                    }
                }
                reader.readAsDataURL(this.files[0]);
            })
        $('#btnCrop').on('click', function () {
            var img = cropper.getDataURL();
            $('.cropped').html('');
            $('.cropped').append('<img src="' + img + '" align="absmiddle" style="width:64px;margin-top:4px;border-radius:64px;box-shadow:0px 0px 12px #7E7E7E;" ><p>64px*64px</p>');
            $('.cropped').append('<img src="' + img + '" align="absmiddle" style="width:128px;margin-top:4px;border-radius:128px;box-shadow:0px 0px 12px #7E7E7E;"><p>128px*128px</p>');
            $('.cropped').append('<img src="' + img + '" align="absmiddle" style="width:180px;margin-top:4px;border-radius:180px;box-shadow:0px 0px 12px #7E7E7E;"><p>180px*180px</p>');
        })
        $('#btnZoomIn').on('click', function () {
            cropper.zoomIn();
        })
        $('#btnZoomOut').on('click', function () {
            cropper.zoomOut();
        })
    });

    function submitClick() {
        var img = cropper.getBlob();
        var formdata = new FormData();
        formdata.append("avatarfile", img);
        formdata.append("rowKey", $("#rowKey").val());
        $.ajax({
            url: ctx + "upms/user/updateAvatar",
            data: formdata,
            type: "post",
            processData: false,
            contentType: false,
            success: function (result) {
                $.operate.saveSuccess(result);
            }
        })
    }
</script>
</body>
</html>
